package com.by.common.crud;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.by.common.bean.PageBean;
import com.by.common.bean.PageMap;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.Map;

public class BaseServiceImpl<M extends BaseMapper, T> implements BaseService<T> {

    @Autowired(required = false)
    protected M baseMapper;

    @Override
    public PageBean query(Map map) {
        PageMap pageMap = new PageMap(map);
        IPage<T> page = new Page(pageMap.getPage(), pageMap.getPageSize());
        page = baseMapper.selectPage(page, null);

        return new PageBean(page.getCurrent(), page.getSize(), page.getTotal(), page.getRecords());

    }

    @Override
    public int save(T entity) {
        return baseMapper.insert(entity);
    }

    @Override
    public int update(T entity) {
        return baseMapper.updateById(entity);
    }

    @Override
    public int delete(Serializable id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public T get(Serializable id) {
        return (T) baseMapper.selectById(id);
    }
}
